package com.hyfd.task;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.hyfd.common.utils.GetUtil;
import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

@Component
public class LangShuoFlowTask {

	@Autowired
	ProviderPhysicalChannelDao providerPhysicalChannelDao;//物理通道信息
	@Autowired
	OrderDao orderDao;//订单
	@Autowired
	RabbitMqProducer mqProducer;//消息队列生产者
	private static Logger log = Logger.getLogger(BoLuoMiNewFlowTask.class);
	
//	@Scheduled(fixedDelay = 60000)
	public void queryBoLuoMiNewOrder(){
		Map<String,Object> map = new HashMap<String,Object>();

		int flag = -1;
		try{
			String id = "0000000105";
			Map<String,Object> channel = providerPhysicalChannelDao.selectByPrimaryKey(id);//获取通道的数据
			String defaultParameter = (String) channel.get("default_parameter");//默认参数
			Map<String,String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
			String queryOrderUrl = paramMap.get("queryOrderUrl")+"";//查询地址
			String username  = paramMap.get("username");// 账户名
            String apikey = paramMap.get("apikey");
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("dispatcherProviderId", id);
			param.put("status", "1");
			List<Map<String,Object>> orderList = orderDao.selectByTask(param);
			for(Map<String,Object> order : orderList){
				String orderId = order.get("orderId")+"";
				map.put("orderId", orderId);
				String sign =MD5.ToMD5("username="+username+"&apikey="+apikey); 
	            
	    		String params ="?username="+username+"&user_order_id="+orderId+"&sign=" + sign.toUpperCase();
	    		
	    		String getUrl = queryOrderUrl+params;
	    		String retMsg = ToolHttp.get(false, getUrl);
				if(null!=retMsg&&!"".equals(retMsg)){
					JSONObject jObject = JSONObject.parseObject(retMsg);
	                String resultCode = jObject.getString("errcode");
	                String result = jObject.getString("order");
	                if ("0".equals(resultCode))
	                {
	                	JSONObject resultObj = JSONObject.parseObject(result);
	                	String status = resultObj.getString("status");
	                	String msg = resultObj.getString("msg");
	                	
	                	if("0".equals(status)||"1".equals(status)||"2".equals(status)){
	                		continue;
	                	}else if("4".equals(status)){
	                		flag=1;
	                		map.put("resultCode", status + ":" + msg);
	                	}else if("8".equals(status)){
	                		flag=0;
	                		map.put("resultCode", status + ":" + msg);
	                	}
	                }else{
	                	continue;
	                }
				}else{
					continue;
				}
				map.put("status", flag);
				mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
			}
		}catch(Exception e){
			log.error("langshuo流量查询Task出错"+e);
		}
	}

}
